package com.ys.controller.api.kefu;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ys.config.MenuId;
import com.ys.entity.business.TpProductBasis;
import com.ys.entity.kefu.TpGaugeMaintenance;
import com.ys.entity.sys.YsCol;
import com.ys.service.kefu.TpGaugeMaintenanceService;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.UploadService;
import com.ys.util.ExportExcelUtils;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;

@Controller
@RequestMapping("/api/kefu/tpGaugeMaintenance")
public class ApiInterTpGaugeMaintenanceController {
	
	@Autowired
	private CommonApiService<TpGaugeMaintenance> commonApiService;
	@Autowired
	private TpGaugeMaintenanceService tpGaugeMaintenanceService;
	/**
	 * 设置查询的cond的sql设置
	 * @param cond
	 * @return
	 */
	public TpGaugeMaintenance setSeachSql(TpGaugeMaintenance cond){

		cond.addField("B.code code,B.name name");


		cond.addLeftJoin("TP_ProductActive B on B.code=A.code");



		return  cond;
	}
	//处理返回的数据
	public List<Map<String, Object>> setList(List<Map<String, Object>> resDate){
		List<Map<String, Object>> tableDate = new ArrayList<Map<String, Object>>();



		
		return tableDate;
	}
	
	/**
	 * 查询
	 * @param model
	 * @param request
	 * @param response
	 * @param cond
	 * @return
	 */
	@RequestMapping("/search")
    @ResponseBody
	public LayuiTableVoJA search(@ModelAttribute TpGaugeMaintenance cond){
		return commonApiService.searchLay(cond);
	}
	
	/**
	 * 查询(弹出框使用)
	 */
	@RequestMapping("/searchForModal")
    @ResponseBody
	public LayuiTableVoJA searchForModal(@ModelAttribute TpGaugeMaintenance cond){
		return commonApiService.searchLay(cond);
	}
	
	/**
	 * 导出
	 * @param cond
	 * @return
	 */
	@RequestMapping("/export")
	@ResponseBody
	public R export(@ModelAttribute TpGaugeMaintenance cond){
		try {
			cond.setPage(0).setSize(999999);
			cond = setSeachSql(cond);
			//cond.setOrderby("A.OperateCompanyId ASC,A.PhaseNum ASC ");
			cond.setOrderby("A.Id ASC ");
			//获取数据
			List<Map<String, Object>> listData = cond.queryMap();

			//处理数据
			listData = setList(listData);

			//获取菜单字段数据
			List<YsCol> listField = new YsCol()
					.setMenuId(MenuId.TP_GaugeMaintenance)
					.where("A.Code != 'ope'")
					.setOrderby("A.SortNo asc")
					.queryList();


			ExportExcelUtils<TpGaugeMaintenance> util = new ExportExcelUtils<>(new TpGaugeMaintenance());

			String toFilePath = util.exportExcel(listData, listField, "箱规维护");
			UploadService.UploadDto dto = commonApiService.uploadLocalToServer(toFilePath);
			return R.ok(dto.getUrl());
		}catch (Exception e){
			e.printStackTrace();
			return R.error("导出失败，请稍后重试！");
		}
	}

	/**
	 * 新增
	 */
	@RequiresPermissions("tpGaugeMaintenance:add")
	@PostMapping("/add")
    @ResponseBody
	public R add(@RequestParam Map<String, Object> params,
			@ModelAttribute TpGaugeMaintenance cond,
			String conditions){
		cond.setFields("B.Name name");
		cond.addLeftJoin("TP_ProductActive B on B.code=A.code");
		TpProductBasis tpProductBasis = new TpProductBasis().setCode(String.valueOf(params.get("code"))).queryFirst();
		params.put("name",tpProductBasis.getName());
		return tpGaugeMaintenanceService.add(params);
	}


	/**
	 * 删除
	 */
	@RequiresPermissions("tpGaugeMaintenance:delete")
	@PostMapping("/deleteTrue")
    @ResponseBody
	public R deleteTrue(@RequestParam Map<String, Object> params) {

		try {
			//删除
			for(int i=0;i<params.size();i++){
				return tpGaugeMaintenanceService.deleteTpGaugeMaintenance(params);
			}
			return R.ok("删除成功!");
		}catch (Exception e){
			e.printStackTrace();
			return R.error("删除失败，请稍后重试！");
		}

	}
	/**
	 * 修改
	 */
	@RequiresPermissions("tpGaugeMaintenance:edit")
	@PostMapping("/edit")
	@ResponseBody
	public R edit(@RequestParam Map<String, Object> params){
		try {
			return tpGaugeMaintenanceService.edit(params);
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
		}
	}
}
